import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int[][] array = {{1, 2, 3}, {4, 5, 6}};

        for (int[] tmp : array) {
            for (int x : tmp) {
                System.out.println(x + " ");
            }
            System.out.println();
        }
        System.out.println();

        System.out.println(Arrays.deepToString(array));

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }





    public static void main8(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 18, 9};

        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void bubbleSort(int[] array) {                    //实现数组的冒泡排序
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                }
            }
        }
    }

    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            swap(array, left, right);
            left++;
            right--;
        }
    }

    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }





    public static void main7(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5, 6, 7, 18, 9};
        int[] array2 = {1, 2, 3, 4, 5, 6, 17, 18, 9};

        System.out.println(Arrays.equals(array1, array2));         //判断两个数组是否相等，返回的是布尔类型的值

        int[] array3 = new int[10];
        Arrays.fill(array3,7);                                //填充数组
        System.out.println(Arrays.toString(array3));

        Arrays.fill(array3, 3, 5, 8);         //局部填充数组，[3,5)
        System.out.println(Arrays.toString(array3));
    }





    public static void main6(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 18, 9};

        Arrays.sort(array);                                         //先排序
        System.out.println(Arrays.toString(array));

        System.out.println(Arrays.binarySearch(array, 7));      //再进行二分查找
    }





    public static void main5(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};

        System.out.println(binarySearch(array, 7));         //使用自己写的方法来进行二分查找

        System.out.println(Arrays.binarySearch(array, 7));  //使用Java提供的方法来进行二分查找
    }

    public static int binarySearch(int[] array, int key) {       //二分查找
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (array[mid] < key) {
                left = mid + 1;
            } else if (array[mid] > key) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }





    public static void main4(String[] args) {                //求数组的平均值
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int sum = 0;
        for (int x : array) {
            sum += x;
        }
        System.out.println(sum * 1.0 / array.length);
    }




    public static void main3(String[] args) {
        int[] array = {12, 13, 14, 15, 16, 17, 18};

        /**
         * 参数1：要拷贝的数组
         * 参数2：要拷贝数组的长度
         */
        int[] copy1 = Arrays.copyOf(array, array.length);                     //拷贝数组
        System.out.println(Arrays.toString(copy1));

        int[] copy2 = Arrays.copyOf(array, 2 * array.length);       //可用于给数组扩容
        System.out.println(Arrays.toString(copy2));

        int[] copy3 = Arrays.copyOfRange(array, 2, 4);               //数组的部分拷贝，[2,4)
        System.out.println(Arrays.toString(copy3));

        int[] copy4 = new int[array.length];
        System.arraycopy(array, 0, copy4, 0, array.length);     //拷贝数组的另一种方法
        System.out.println(Arrays.toString(copy4));
    }





    public static void main2(String[] args) {
        int[] array = {888, 777, 666, 555};
        int[] copy = copyOf(array);
        System.out.println(Arrays.toString(copy));
    }

    public static int[] copyOf(int[] array) {           //拷贝数组
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;
    }





    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4};
        String ret = myToString(array);
        System.out.println(ret);
    }

    public static String myToString(int[] array) {        //模拟实现Arrays.toString()
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str = str + array[i];
            if (i != array.length - 1) {
                str = str + ", ";                       //合理使用字符串的拼接
            }
        }
        str = str + "]";
        return str;
    }
}
